package net.aiotos.common.dto;


import com.alibaba.fastjson.JSONObject;
import net.aiotos.common.exception.*;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class HeartbeatDTO {
    private final Integer code;
    private final String msg;
    private final Long servertime;

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    /**
     * 异常检测，统一处理非0返回void
     */
    public void check() throws Exception {
        if(code == ErrorEnum.Successfull.code) {
            return;
        }

        if(code == ErrorEnum.SelfOffline.code) {
            throw new SelfOfflineError();
        }
        if(code == ErrorEnum.AccountNotRegister.code) {
            throw new AccountNotRegisterError();
        }
        if(code == ErrorEnum.IONodeNotExist.code) {
            throw new IONodeNotExistError();
        }
        if(code == ErrorEnum.DataNotExist.code) {
            throw new DataNotExistError("");
        }
        if(code == ErrorEnum.RequestNotAuth.code) {
            throw new RequestNotAuthError("");
        }
//        if(code == ErrorEnum.Unknown) {
//            throw new Exception("未定义:" + rawJson);
//        }
        throw new Exception(JSONObject.toJSONString(this));
    }
}
